package top.glkj.hms.utils;

import org.springframework.stereotype.Component;
import top.glkj.hms.entity.BaseEntity;
import top.glkj.hms.entity.Role;
import top.glkj.hms.entity.User;

import java.util.Date;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * @author : xgl
 * @version : 0.0.1
 * @date :2020/11/24 0:28
 */
@Component
public final class StaticValue {
    /**
     * 常用int型常量
     */
    public static final int ZERO = 0;
    public static final int ONE = 1;
    public static final int TWO = 2;
    public static final int THREE = 3;
    public static final int FOUR = 4;
    public static final int FIVE = 5;
    public static final int SIX = 6;
    public static final int SEVEN = 7;
    public static final int EIGHT = 8;
    public static final int NINE = 9;
    public static final int TEN = 10;
    public static final int TWENTY = 20;
    public static final int THIRTY = 30;
    public static final int FORTY = 40;
    public static final int FIFTY = 50;
    public static final int SIXTY = 60;
    public static final int SEVENTY = 70;
    public static final int EIGHTY = 80;
    public static final int NINETY = 90;
    public static final int HUNDRED = 100;
    public static final String EMPTY = "";
    public static final Object NULL = null;
    public static final String BACKSLASH = "\"";
    public static final String COMMA = ",";
    public static final String LOCALHOST = "127.0.0.1";
    public static final byte SECTION_1 = 0x0A;
    public static final byte SECTION_2 = (byte) 0xAC;
    public static final byte SECTION_3 = (byte) 0x10;
    public static final byte SECTION_4 = (byte) 0x1F;
    public static final byte SECTION_5 = (byte) 0xC0;
    public static final byte SECTION_6 = (byte) 0xA8;
    public static final long MAX_LONG_ADDRESS = 4294967295L;
    public static final long TWO_FIVE_FIVE = 255L;
    public static final  long MANY_LONG_ADDRESS = 16777215L;
    public static final long ANY_LONG_ADDRESS = 65535L;
    public static final String DATA_NOT_FOUND = "未查询到匹配数据！";
    public static final String SUCCESS_MESSAGE = "请求成功！";
    public static final String ERROR_MESSAGE = "请求失败！";
    public static final String GET_DATA_SUCCESS = "获取数据成功！";
    public static final String UPDATE_DATA_SUCCESS = "数据更新成功！";
    public static final String ADD_DATA_SUCCESS = "添加数据成功！";
    public static final String DELETE_DATA_SUCCESS = "删除数据成功！";
    public static final String GET_DATA_ERROR = "获取数据失败！";
    public static final String UPDATE_DATA_ERROR = "数据更新失败！";
    public static final String ADD_DATA_ERROR = "添加数据失败！";
    public static final String DELETE_DATA_ERROR = "删除数据失败！";
    public static final String USERNAME_NOTFOUND = "用户名未找到！";
    public static final String USER_NOTFOUND = "用户未找到";
    public static final String USERNAME_OR_PASSWORD_ERROR = "用户名或密码错误！";
    public static final String USER_IS_LOCKED = "用户被锁定！";
    public static final String USER_IS_UNLOCKED = "用户解除锁定但未激活账户！";
    public static final String USER_IS_DELETE = "用户已注销或被删除！如有疑问，请联系管理员。";
    public static final String USER_IS_NOT_CERTIFIED = "未认证登录！";
    public static final String USER_NOT_PERMISSION = "无权限访问！";
    public static final String PASSWORD_IS_FAIL = "密码错误！";
    public static final String COMPARE_PASSWORD_FAIL = "原密码输入错误！";
    public static final String NOT_UPDATE_DATA = "没有待更新数据！";
    public static final String UPDATE_IS_EXCEPTION = "更新数据异常！";
    public static final String NOT_ROLE_DATA = "未找到角色数据！";
    public static final String FAIL = "失败";
    public static final String ERROR = "错误";
    public static final String NOT_FOUND = "未找到";
    public static final String EXCEPTION = "异常";
    public static final String PREFIX_ORDER = "GLKJ_HMS:";
    public static final Role EMPTY_ROLE = new Role(-1L);
    public static BaseEntity getBaseEntity(BaseEntity baseEntity) {
        baseEntity = new BaseEntity();
        baseEntity.setCreateTime(new Date());
        baseEntity.setDel(false);
        baseEntity.setUpdateTime(new Date());
        return baseEntity;
    }
    private static  String LAST_KEY = "";
    private static final Set<String> SET_NUMBER = new HashSet<>();
    public synchronized static String getOrderNumber(User user){
        String sysTime = String.valueOf(System.currentTimeMillis()).substring(3);
        if (!LAST_KEY.equals(sysTime)){
            SET_NUMBER.clear();
        }else {
            LAST_KEY = sysTime;
        }
        String res = sysTime+String.format("%03d",user.getId()%getOneToHundred());
        while (!SET_NUMBER.add(res)){
            res = sysTime+String.format("%03d",user.getId()%getOneToHundred());
        }
        return res;
    }

    public static int getOneToHundred(){
        Random random = new Random();
        int v =random.nextInt()%100;
        if (v<=StaticValue.ZERO){return StaticValue.FIFTY;}
        return v;
    }

}
